package com.api.regression.group.editdocument;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.zyncro.api.IGroupApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.DocumentApi;

@ContextConfiguration(locations = { "classpath:/group/group-context.xml" })
public class EditDocument extends AbstractApiTest {

	@Value("#{editgroupdocuments['user.editor'].appId}")
	private String userUrn;

	@Value("#{editgroupdocuments['user.editor'].password}")
	private String userPassword;

	@Value("#{editgroupdocuments['group.toEdit'].urn}")
	private String groupUrn;

	@Value("#{editgroupdocuments['group.toEdit'].folderOwnedUrn}")
	private String folderUrn;

	@Value("#{editgroupdocuments['group.toEdit'].fileOwnedUrn}")
	private String fileUrn;

	@Value("#{editgroupdocuments['group.toEdit'].linkOwnedUrn}")
	private String linkUrn;

	@Value("#{editgroupdocuments['group.toEdit'].shortcutOwnedUrn}")
	private String shortcutUrn;

	private String folderNameBefore;

	private String fileNameBefore;

	private String linkNameBefore;

	private String shortcutNameBefore;

	@BeforeClass
	protected void loginUser() throws Exception {

		getApi().login(userUrn, userPassword);

		DocumentApi folder = getApi().getService(IGroupApiService.class).getDocument(groupUrn, folderUrn).getObject();
		this.folderNameBefore = folder.getName();

		DocumentApi file = getApi().getService(IGroupApiService.class).getDocument(groupUrn, fileUrn).getObject();
		this.fileNameBefore = file.getName();

		DocumentApi link = getApi().getService(IGroupApiService.class).getDocument(groupUrn, linkUrn).getObject();
		this.linkNameBefore = link.getName();

		DocumentApi shortcut = getApi().getService(IGroupApiService.class).getDocument(groupUrn, shortcutUrn).getObject();
		this.shortcutNameBefore = shortcut.getName();
	}

	@Test
	public void testEditFolder() throws ZyncroApiException {

		String name = "Name after";
		String description = "Description after";
		String attributes = null;

		getApi().getService(IGroupApiService.class).editDocument(groupUrn, folderUrn, name, description, attributes);

		DocumentApi folder = getApi().getService(IGroupApiService.class).getDocument(groupUrn, folderUrn).getObject();

		Assert.assertEquals(folder.getName(), name, "Folder name");
		Assert.assertEquals(folder.getDescription(), description, "Folder description");
	}

	@Test
	public void testEditFile() throws ZyncroApiException {

		String name = "Name after";
		String description = "Description after";
		String attributes = null;

		getApi().getService(IGroupApiService.class).editDocument(groupUrn, fileUrn, name, description, attributes);

		DocumentApi file = getApi().getService(IGroupApiService.class).getDocument(groupUrn, fileUrn).getObject();

		Assert.assertEquals(file.getName(), name, "File name");
		Assert.assertEquals(file.getDescription(), description, "File description");
	}

	@Test
	public void testEditLink() throws ZyncroApiException {

		String name = "Name after";
		String description = "Description after";
		String attributes = null;

		getApi().getService(IGroupApiService.class).editDocument(groupUrn, linkUrn, name, description, attributes);

		DocumentApi link = getApi().getService(IGroupApiService.class).getDocument(groupUrn, linkUrn).getObject();

		Assert.assertEquals(link.getName(), name, "Link name");
		Assert.assertEquals(link.getDescription(), description, "Link description");
	}

	@Test
	public void testEditShortcut() throws ZyncroApiException {

		String name = "Name after";
		String description = "Description after";
		String attributes = null;

		getApi().getService(IGroupApiService.class).editDocument(groupUrn, shortcutUrn, name, description, attributes);

		DocumentApi shortcut = getApi().getService(IGroupApiService.class).getDocument(groupUrn, shortcutUrn).getObject();

		Assert.assertEquals(shortcut.getName(), name, "Shortcut name");
		Assert.assertEquals(shortcut.getDescription(), description, "Shortcut description");
	}

	@AfterClass
	public void restoreDocumentsValues() throws Exception {

		getApi().getService(IGroupApiService.class).editDocument(groupUrn, folderUrn, folderNameBefore, null, null);
		getApi().getService(IGroupApiService.class).editDocument(groupUrn, fileUrn, fileNameBefore, null, null);
		getApi().getService(IGroupApiService.class).editDocument(groupUrn, linkUrn, linkNameBefore, null, null);
		getApi().getService(IGroupApiService.class).editDocument(groupUrn, shortcutUrn, shortcutNameBefore, null, null);
	}
}
